package com.weitu.archives.personArchives.service.impl;

import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.archives.personArchives.entity.SafetyAssessmentRewardPunishmentLedger;
import com.weitu.archives.personArchives.entity.SafetyAsssessmentRewardPunishmentLedgerRelation;
import com.weitu.archives.personArchives.mapper.SafetyAssessmentRewardPunishmentLedgerMapper;
import com.weitu.archives.personArchives.service.SafetyAssessmentRewardPunishmentLedgerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/5/15.
 */
@Service
public class SafetyAssessmentRewardPunishmentLedgerServiceImpl implements SafetyAssessmentRewardPunishmentLedgerService {

    private final SafetyAssessmentRewardPunishmentLedgerMapper safetyAssessmentRewardPunishmentLedgerMapper;

    @Autowired
    public SafetyAssessmentRewardPunishmentLedgerServiceImpl(SafetyAssessmentRewardPunishmentLedgerMapper safetyAssessmentRewardPunishmentLedgerMapper){
        this.safetyAssessmentRewardPunishmentLedgerMapper=safetyAssessmentRewardPunishmentLedgerMapper;
    }

    @Override
    public int queryCount(Map<String, Object> map) {
        return this.safetyAssessmentRewardPunishmentLedgerMapper.queryCount(map);
    }

    @Override
    public List<SafetyAssessmentRewardPunishmentLedger> queryList(Map<String, Object> map) {
        return this.safetyAssessmentRewardPunishmentLedgerMapper.queryList(map);
    }

    @Override
    @Transactional
    public boolean insertSafetyAssessmentRewardPunishmentLedgerAndRelation(SafetyAssessmentRewardPunishmentLedger punishmentLedger, List<SafetyAsssessmentRewardPunishmentLedgerRelation> ledgerRelationList) {
        this.safetyAssessmentRewardPunishmentLedgerMapper.deleteSafetyAssessmentRewardPunishmentLedgerRelation("");
        if(ledgerRelationList.size()>0)
            this.safetyAssessmentRewardPunishmentLedgerMapper.insertSafetyAssessmentRewardPunishmentLedgerRelation(ledgerRelationList);
        int count=this.safetyAssessmentRewardPunishmentLedgerMapper.insertSafetyAssessmentRewardPunishmentLedger(punishmentLedger);
        boolean flag= AccessoryUtil.saveAccerssoryInfo(punishmentLedger.getId());
        boolean flag2=false;
        if(count==1){
            flag2=true;
        }
        return flag&&flag2;
    }

    @Override
    public SafetyAssessmentRewardPunishmentLedger queryRewardPunishLedgerById(String id) {
        return this.safetyAssessmentRewardPunishmentLedgerMapper.queryRewardPunishLedgerById(id);
    }

    @Override
    public List<SafetyAsssessmentRewardPunishmentLedgerRelation> queryListRelationById(String id) {
        return this.safetyAssessmentRewardPunishmentLedgerMapper.queryListRelationById(id);
    }

    @Override
    @Transactional
    public boolean updateSafetyAssessmentRewardPunishmentLedgerAndRelation(SafetyAssessmentRewardPunishmentLedger punishmentLedger, List<SafetyAsssessmentRewardPunishmentLedgerRelation> ledgerRelationList) {
        this.safetyAssessmentRewardPunishmentLedgerMapper.deleteSafetyAssessmentRewardPunishmentLedgerRelation(punishmentLedger.getId());
        if(ledgerRelationList.size()>0)
            this.safetyAssessmentRewardPunishmentLedgerMapper.insertSafetyAssessmentRewardPunishmentLedgerRelation(ledgerRelationList);
        int count=this.safetyAssessmentRewardPunishmentLedgerMapper.updateSafetyAssessmentRewardPunishmentLedger(punishmentLedger);
        boolean flag= AccessoryUtil.saveAccerssoryInfo(punishmentLedger.getId());
        boolean flag2=false;
        if(count==1){
            flag2=true;
        }
        return flag&&flag2;
    }

    @Override
    public int deleteSafetyAssessmentRewardPunishmentLedger(String id) {
        return this.safetyAssessmentRewardPunishmentLedgerMapper.deleteSafetyAssessmentRewardPunishmentLedger(id);
    }

    @Override
    public SafetyAsssessmentRewardPunishmentLedgerRelation selectRelationById(String id) {
        return this.safetyAssessmentRewardPunishmentLedgerMapper.selectRelationById(id);
    }

    @Override
    public int updateRelation(SafetyAsssessmentRewardPunishmentLedgerRelation realation) {
        return this.safetyAssessmentRewardPunishmentLedgerMapper.updateRelation(realation);
    }

    @Override
    public int insertRelation(SafetyAsssessmentRewardPunishmentLedgerRelation realation) {
        return this.safetyAssessmentRewardPunishmentLedgerMapper.insertRelation(realation);
    }
}
